/**
 * Created by gongxun on 16/11/18.
 */

var KwxTableLayer = GameCCSBase.extend({
    mKwxTableUILayer : null,
    mKwxCardLayer : null,
    mKwxReplayLayer : null,
    mKwxJuEndedEventkwx : null,
    mKwxTableShanglouLayer : null,
    mTestLayer : null,

    //牌桌状态 0录像 1正常打牌
    mPlayerType : null,

    //游戏设置
    mTableConfig : null,

    //游戏密码
    mTableEnterPwd : null,

    //包房名称
    mGroupName : null,

    //包房id 通过退出牌桌消息获得
    mGroupId : null,

    //录像id
    mReplayId : null,

    //记录当前局本地打了几盘了
    mPlayGameCount : null,

    //是否是房主
    mIsRoomCreator : null,

    //倒计时
    Text_leftTime : null,
    mTimeLeft : null,

    //该谁出牌
    Image_nowSeat : null,
    Image_nowSeatNull : null,

    //龙
    Image_bgEffect : null,

    //上楼
    Image_tableShanglou : null,

    ctor : function(){
        this._super();
        mo.kwxTable = this;
        this.loadCocostudio("res/ccs/KwxTable.json");
        this.mIsRoomCreator = false;
        this.Text_leftTime = mo.seekWidgetByName(this.ccsNode, "Text_leftTime");
        this.Image_nowSeat = mo.seekWidgetByName(this.ccsNode, "Image_nowSeat");
        this.Image_nowSeatNull = mo.seekWidgetByName(this.ccsNode, "Image_nowSeatNull");
        this.Image_bgEffect = mo.seekWidgetByName(this.ccsNode, "Image_bgEffect");
        this.Image_bgEffect.setVisible(false);
        this.Image_tableShanglou = mo.seekWidgetByName(this.ccsNode, "Image_tableShanglou");
        this.Image_tableShanglou.setVisible(false);

        //初始化打牌相关
        mo.kwxCardHelper.setCardScaleInfo();

        //testLayer
        this.mTestLayer = new cc.Layer();
        this.addChild(this.mTestLayer);

        //打牌相关
        this.mKwxCardLayer = new KwxCardLayer();
        this.addChild(this.mKwxCardLayer);

        //上楼
        this.mKwxTableShanglouLayer = new KwxTableShanglouLayer();
        this.mKwxTableShanglouLayer.mText_shanglou = this.Image_tableShanglou;
        this.mKwxTableShanglouLayer.setShanglouLocationNode(mo.seekWidgetByName(this.ccsNode, "Text_tableRoomId"));
        this.addChild(this.mKwxTableShanglouLayer);

        //ui
        this.mKwxTableUILayer = new KwxTableUILayer();
        this.addChild(this.mKwxTableUILayer);

        //录像
        this.mKwxReplayLayer = new KwxReplayLayer();
        this.addChild(this.mKwxReplayLayer);

        //2人场重新布局
        this.player2pReLoadUI();
    },

    onEnter : function(){
        this._super();
        this.runAction(cc.sequence(cc.delayTime(1), cc.callFunc(function(){
            mo.soundHelper.playMusicGame();
        })));

        mo.eventHelper.add("KwxForceExitTableResponsekwxOK", this, function () {
            if (mo.kwxTableStatus.mTableStatus.LeftPlayTimes == mo.kwxTable.mTableConfig.maxpan){
                mo.showMsgDialogOk("退出牌局成功", function(){
                    this.quitKwxGame();
                }, this);
            }
        });

        //背景
        this.setTableBg();
        mo.eventHelper.add("changeKwxTableBg", this, function(){
            this.setTableBg();
        });

        //倒计时
        this.mTimeLeft = 0;
        this.schedule(this.setUpdateTimeLeft, 0.2);
    },

    onExit : function(){
        mo.eventHelper.remove(this);
        this._super();
    },

    setTableBg : function(){
        var Image_bg = mo.seekWidgetByName(this.ccsNode, "Image_bg");
        Image_bg.loadTexture("res/ccs/table/tableBg" + mo.gameUserInfo.mGameKwxBgIndex + ".png")
    },

    //播放录像
    loadReplay : function(rePlaykwx, replayId){
        this.mPlayerType = 0;
        this.mPlayGameCount = 0;
        this.resetTable();

        //录像的规则
        mo.kwxTable.setKwxConfig(rePlaykwx.Result.CfgJson, "", "");

        //打开录像界面 并且播放
        this.mKwxReplayLayer.setVisible(true);
        this.mKwxReplayLayer.setReplayId(replayId);
        this.mKwxReplayLayer.onStart();

        //隐藏录像时候的ui
        this.mKwxTableUILayer.setIsReplay(true);

        this.mReplayId = replayId;
        mo.kwxTableStatus.setReplayMsg(rePlaykwx);
        mo.kwxTableStatus.playReplay();
    },

    //正常打牌
    loadPlayCard : function(){
        this.mPlayerType = 1;
        this.mPlayGameCount = 0;
        this.resetTable();

        //隐藏录像界面
        this.mKwxReplayLayer.setVisible(false);

        //隐藏录像时候的ui
        this.mKwxTableUILayer.setIsReplay(false);
    },

    //显示代开房人
    setShowRoomCreate : function(user){
        if (user != null){
            var kwxDaikaifangWidget = new KwxDaikaifangWidget();
            kwxDaikaifangWidget.setUserInfo(user);
            this.ccsNode.addChild(kwxDaikaifangWidget);
            kwxDaikaifangWidget.setPosition(cc.p(mo.gameSize.x / 2 - 54, mo.gameSize.y - 65));

            //2人场位置
            if (mo.gameMsgManager.kwxGameModel == 2){
                kwxDaikaifangWidget.setPosition(cc.p(mo.gameSize.x - 110, mo.gameSize.y - 160));
            }
        }
    },

    //处理货币结算
    onHandlerTableResult : function(){
        mo.kwxTableStatus.handlerTableResult();
    },

    setInhandCards : function(cardNumss){
        //清空
        this.resetTable();

        for (var i = 0; i < 3; i++) {
            this.mKwxCardLayer.setInhandCards(i, cardNumss[i]);
        }
    },

    addInhandCard : function(seat, cardNum){
        this.mKwxCardLayer.addInhandCard(seat, cardNum);
    },

    setDealCard : function(seat, cardNum){
        this.mKwxCardLayer.setDealCard(seat, cardNum);
    },

    //openType
    //2 碰 3张
    //4 明杠 4张
    //5 暗杠 4张
    //6 补杠 1张
    setOpenCard : function(seat, cardNums, openType){
        this.mKwxCardLayer.setOpenCard(seat, cardNums, openType);
    },

    //重置牌桌
    resetTable : function(){
        this.mKwxCardLayer.removeAllCard();
        this.mKwxCardLayer.setLastPointerShow(false);
    },

    ///ui
    setCurrentPlayer : function(seat, isOperating){
        var rotates = [0, 270, 90.1];
        var toRotate = rotates[seat];

        //2人场情况
        if (mo.gameMsgManager.kwxGameModel == 2 && seat != 0){
            toRotate = 180;
        }

        this.Image_nowSeat.stopAllActions();
        if (seat >= 0 && seat <= 2){
            this.Image_nowSeat.setVisible(true);
            this.Image_nowSeat.runAction(cc.sequence(cc.rotateTo(0.2, toRotate)));
        }else{
            this.Image_nowSeat.setVisible(false);
        }

        if (isOperating){
            this.Image_nowSeat.setVisible(false);
            this.Image_nowSeatNull.setVisible(true);
        }else{
            this.Image_nowSeatNull.setVisible(false);
        }
    },

    //更新step
    updateTableStep : function(step){
        //准备界面
        this.mKwxTableUILayer.showReady(step == 0 && this.mPlayGameCount == 0);

        //显示能否踢人
        this.mKwxTableUILayer.setShowKickBtn(mo.kwxTableStatus.mTableStatus.LeftPlayTimes != this.mTableConfig.maxpan);

        //该谁打牌
        mo.seekWidgetByName(this.ccsNode, "Panel_curPoint").setVisible(step == 2);
        mo.seekWidgetByName(this.ccsNode, "Panel_leftCards").setVisible(step == 2);

        //飘
        if (step == 1){
            var isPiao = false;
            var seatUser = mo.kwxTableStatus.mTableStatus["SeatUser" + mo.kwxTableStatus.mTableStatus.MySeat];
            if (seatUser){
                for (var i = 0; i < seatUser.NewActionList.length; i++){
                    if (seatUser.NewActionList[i].code == 1){
                        isPiao = true;
                        break;
                    }
                }
            }
            this.mKwxTableUILayer.mKwxTablePiaoLayer.setMySelectedPiao(isPiao);
        }else{
            this.mKwxTableUILayer.mKwxTablePiaoLayer.setVisible(false);
        }

        //剩余牌数
        if (step == 2){
            //剩余牌数
            this.setLeftCardsCount(mo.kwxTableStatus.mTableStatus.LeftCards);

            //倒计时刷新
            this.mTimeLeft = 15;
        }

        //剩余牌局数
        this.setTableIdAndLeftPlay(mo.kwxTableStatus.mTableStatus.TableID, mo.kwxTableStatus.mTableStatus.LeftPlayTimes);

        //控制游戏的结算画面
        if (step == 0){
            var selfSeatUser = mo.kwxTableStatus.mTableStatus["SeatUser" + mo.kwxTableStatus.mTableStatus.MySeat];
            if (selfSeatUser.State == 1){
                this.mKwxTableUILayer.mKwxTableEndLayer.setVisible(false);
            }
        }else{
            this.mKwxTableUILayer.mKwxTableEndLayer.setVisible(false);
        }

        //关闭显示2人场
        if (step != 0){
            mo.seekWidgetByName(this.ccsNode, "Text_ppOnlyRule").setVisible(false);
        }

        //显示上楼操作
        if (mo.kwxTableStatus.mTableStatus.BasePoint == 1){
            mo.gameHelper.loadTextureImage(this.Image_tableShanglou, "res/ccs/table/img_shanglou1.png");
            this.Image_tableShanglou.setVisible(false);
        }else if(mo.kwxTableStatus.mTableStatus.BasePoint == 2 && this.mTableConfig.sl == 1){
            mo.gameHelper.loadTextureImage(this.Image_tableShanglou, "res/ccs/table/img_shanglou2.png");
            this.Image_tableShanglou.setVisible(true);
        }
    },

    //设置剩余牌数
    setLeftCardsCount : function(leftCardsCount){
        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_leftCards"), "" + leftCardsCount);
    },

    //设置规则
    setKwxConfig : function(CfgJson, tablePassWord, groupName){
        //密码
        if (tablePassWord != null){
            this.mTableEnterPwd = tablePassWord;
            if (mo.gameHelper.developerVersion == "2.0"){
                this.mTableEnterPwd = null;
            }
        }

        if (groupName != null){
            this.mGroupName = groupName;
        }

        //规则
        if (CfgJson == ""){
            //没有规则
            this.mTableConfig = null;
        }else{
            this.mTableConfig = JSON.parse(CfgJson);
        }

        var Panel_ruleInfo = mo.seekWidgetByName(this.ccsNode, "Panel_ruleInfo");
        if (this.mTableConfig){
            Panel_ruleInfo.setVisible(true);

            var ruleStr = mo.gameHelper.getRuleConfigSummary(this.mTableConfig, "kwx", mo.gameMsgManager.kwxGameModel == 1 ? 150000 : 650000);
            ruleStr = ruleStr.replace(/，/g, " ");
            mo.seekWidgetByName(Panel_ruleInfo, "TextFull_tableRule").setString(ruleStr);
            // mo.gameHelper.setTextString(mo.seekWidgetByName(Panel_ruleInfo, "TextFull_tableRule"), ruleStr);
        }
        else{
            Panel_ruleInfo.setVisible(false);
        }

        //判断 如果是十堰上楼 就插入上楼界面啰
        if (this.mTableConfig != null){
            if (this.mTableConfig.ruleid == 3 && this.mTableConfig.sl == 1){
                this.mKwxTableShanglouLayer.onActivation();
                this.Image_tableShanglou.setVisible(true);
            }
        }
    },

    //设置牌桌号码 剩余局数
    setTableIdAndLeftPlay : function(tableId, leftPlay){
        //房间号
        if (mo.gameHelper.developerVersion == "3.0"){
            if (tableId != null){
                var tableStr = "房间号:" + tableId;
                if (this.mTableConfig != null && this.mTableConfig.pwd == 1){
                    tableStr = "密码:" + this. mTableEnterPwd;
                }

                var Text_tableRoomId = mo.seekWidgetByName(this.ccsNode, "Text_tableRoomId");
                mo.gameHelper.setTextString(Text_tableRoomId, tableStr);
            }
        }else if (mo.gameHelper.developerVersion == "2.0"){
            if (tableId != null){
                var Text_tableRoomId = mo.seekWidgetByName(this.ccsNode, "Text_tableRoomId");
                mo.gameHelper.setTextString(Text_tableRoomId, "房间号:" + tableId);
            }
        }

        //剩余局数
        if (leftPlay != null){
            var currentJu = leftPlay;
            if (currentJu < 0){
                currentJu = 0;
            }
            var shengyuJu = currentJu + "";
            var Text_leftJu = mo.seekWidgetByName(this.ccsNode, "Text_leftJu");
            mo.gameHelper.setTextString(Text_leftJu, shengyuJu);
        }
    },

    //倒计时
    setUpdateTimeLeft : function(time){
        this.mTimeLeft -= time;
        if (this.mTimeLeft < 0){
            this.mTimeLeft = 0;
        }
        mo.gameHelper.setTextString(this.Text_leftTime, "" + parseInt(this.mTimeLeft));
    },

    //邀请玩家
    onInvite : function(){
        mo.gameHelper.inviteGame(this.mTableConfig, "kwx", mo.kwxTableStatus.mTableStatus.TableID);
    },

    //分享录像
    onShareReplay : function(){
        mo.gameHelper.shareReplay(this.mTableConfig, "kwx", mo.kwxTableStatus.mTableStatus.TableID, this.mReplayId);
    },

    //加载大结算
    onShowJuEndedLayer : function(juEndedEventkwx){
        //判断是否需要直接退出 如果剩余局数和设置里面的局数一致 就直接退出
        var ruleJson = JSON.parse(juEndedEventkwx.CfgJson);

        if (mo.kwxTableStatus.mTableStatus.LeftPlayTimes == ruleJson.maxpan){
            mo.showMsgDialogOk("牌局已经解散", function(){
                this.quitKwxGame();
            }, this);
        }else{

            this.mKwxJuEndedEventkwx = new KwxTableRoomEndLayer();
            mo.sceneHelper.showEndInfoLayer(this.mKwxJuEndedEventkwx);
            // this.addChild(this.mKwxJuEndedEventkwx);
            this.mKwxJuEndedEventkwx.setRoomEndInfo(juEndedEventkwx, mo.gameUserInfo.uid);

            if (juEndedEventkwx.EndFlag != 1){
                //自然结束 通过当局结算打开
                this.mKwxJuEndedEventkwx.setVisible(false);
            }else{
                this.mKwxJuEndedEventkwx.setVisible(true);
            }
        }
    },

    //播放分数动画
    setResultScore : function(scores){
        this.mKwxTableUILayer.mKwxTableResultLayer.setResultScore(scores);
    },

    quitKwxGame : function(){
        if (mo.gameHelper.developerVersion == "2.0"){
            mo.sceneHelper.runSceneHall();
        }else if (mo.gameHelper.developerVersion == "3.0"){
            mo.sceneHelper.exitGameLayer();
            if (this.mGroupId != null){
                mo.hallLayer.intoGroup(this.mGroupId);
            }
        }
    },

    //播放龙动画
    onPlayTableOpAnim : function(){
        this.Image_bgEffect.stopAllActions();
        this.Image_bgEffect.setVisible(false);
        this.Image_bgEffect.setOpacity(150);
        this.Image_bgEffect.runAction(cc.sequence(
            cc.delayTime(0.55),
            cc.show(),
            cc.fadeOut(1),
            cc.hide()
        ));

        this.runAction(cc.sequence(
            cc.delayTime(0.50),
            cc.moveTo(0.05, cc.p(0, -15)),
            cc.moveTo(0.05, cc.p(0, 0)),
            cc.moveTo(0.03, cc.p(0, -10)),
            cc.moveTo(0.03, cc.p(0, 0))
        ));
    },

    player2pReLoadUI : function(){
        if (mo.gameMsgManager.kwxGameModel == 2){
            ///table
            mo.seekWidgetByName(this.ccsNode, "Panel_leftCards").setPositionY(60);
            mo.seekWidgetByName(this.ccsNode, "Panel_curPoint").setPositionY(60);
            mo.seekWidgetByName(this.ccsNode, "Panel_curPoint").setScale(0.6);
            mo.seekWidgetByName(this.ccsNode, "Image_leftCard").setPositionX(100);
            mo.seekWidgetByName(this.ccsNode, "Image_leftCardBg").setPositionX(-100);
            //规则
            mo.seekWidgetByName(this.ccsNode, "Image_logo").setPositionY(mo.gameSize.y - 110);
            mo.seekWidgetByName(this.ccsNode, "TextFull_tableRule").setPositionX(-mo.gameSize.x / 2 + 190);
            mo.seekWidgetByName(this.ccsNode, "Text_tableRoomId").setPositionX(mo.gameSize.x / 2 - 190);
            mo.seekWidgetByName(this.ccsNode, "Image_tableShanglou").setPositionX(mo.gameSize.x / 2 - 190);
            mo.seekWidgetByName(this.ccsNode, "Text_ppOnlyRule").setVisible(true);
            //replay
            mo.seekWidgetByName(this.mKwxReplayLayer.ccsNode, "Button_replayIdShare").setPosition(cc.p(235, -80));


            ///ui
            //head
            this.mKwxTableUILayer.Image_players[1].setPosition(cc.p(50, mo.gameSize.y - 187.5));
            //tiren
            this.mKwxTableUILayer.Button_kick1.setPosition(cc.p(50 + 35, mo.gameSize.y - 187.5 - 1.5));
            this.mKwxTableUILayer.Button_kick1.loadTextures("res/ccs/table/btn/btn_kick1_0.png", "res/ccs/table/btn/btn_kick1_1.png");

            ///result
            //deal card
            this.mKwxTableUILayer.mKwxTableResultLayer.Image_dealCard[0].setPosition(cc.p(mo.gameSize.x / 2, 250));
            this.mKwxTableUILayer.mKwxTableResultLayer.Image_dealCard[1].setPosition(cc.p(mo.gameSize.x / 2, mo.gameSize.y - 150));
            this.mKwxTableUILayer.mKwxTableResultLayer.Image_dealCard[2].setPosition(cc.p(mo.gameSize.x / 2, mo.gameSize.y - 150));
            //result
            this.mKwxTableUILayer.mKwxTableResultLayer.Image_results[0].setPosition(cc.p(mo.gameSize.x / 2, 250));
            this.mKwxTableUILayer.mKwxTableResultLayer.Image_results[1].setPosition(cc.p(mo.gameSize.x / 2, mo.gameSize.y - 150));
            this.mKwxTableUILayer.mKwxTableResultLayer.Image_results[2].setPosition(cc.p(mo.gameSize.x / 2, mo.gameSize.y - 150));
            //score 显示的时候设置了

            ///end
            this.mKwxTableUILayer.mKwxTableEndLayer.Panel_players[0].setPositionY(40);
            this.mKwxTableUILayer.mKwxTableEndLayer.Panel_players[1].setPositionY(200);
            this.mKwxTableUILayer.mKwxTableEndLayer.Panel_players[2].setPositionY(200);
        }
    }
});
